package com.zxh.mall.controller;

import com.zxh.mall.common.vo.MemberVO;
import com.zxh.mall.interceptor.AuthInterceptor;
import com.zxh.mall.vo.Cart;
import com.zxh.mall.vo.CartItem;
import com.zxh.mall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author zhengxh
 * @version 1.0
 * @date 2024/5/17 - 05 - 17 - 20:11
 * @description com.zxh.mall.controller
 */
@Controller
public class CartController {

    @Autowired
    private ICartService cartService;

    @ResponseBody
    @GetMapping("/getUserCartItems")
    public List<CartItem> getUserCartItems(){
        return cartService.getUserCartItems();
    }

    @GetMapping("/cartList")
    public String queryCartList(Model model){
        Cart cart = cartService.getCartList();
        model.addAttribute("cart",cart);
        return "/cartList";
    }

    /**
     * 加入购物车
     * @return
     */
    @GetMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("num") Integer num,
                          Model model){
        CartItem item = null;
        try {
            item = cartService.addCart(skuId,num);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        model.addAttribute("item",item);
        return "success";
    }
}
